package org.ichow.question.plugins
{
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.MP3Loader;
	import flash.display.DisplayObject;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import org.ichow.debug.Debug;
	import org.ichow.question.Question;
	
	/**
	 * ...
	 * play sound plugin
	 * @author M.ichow
	 */
	public class SoundPlugin extends QuestionPlugin
	{
		public static const VERSION:Number = 1.0;
		public static const API:Number = 1.0;
		//play sound button
		protected var __playBtn : SimpleButton;
		//sound loader
		protected var sound : MP3Loader;
		//sound url
		protected var url : String;
		//repeat times
		protected var repeat : int;
		protected var time : int;
		//
		protected var isClick : Boolean = false;
		
		public function SoundPlugin()
		{
			this.propName = "SoundPlugin";
		}
		
		// ---------------------------------- Protected Methods ---------------------------------- //
		
		override protected function init() : void
		{
			super.init();
			__playBtn = getChild( String( getData( "playName" ) ) ) as SimpleButton;
			
			repeat = getData( "repeat" ) as int;
			time = getData( "time" ) as int;
			url = Question.QUESTION_PATH + getData( "url" ) as String;
			
			sound = new MP3Loader( url , { onComplete: onComplete , onError: onError , autoPlay: false } );
			sound.addEventListener( MP3Loader.SOUND_COMPLETE , onSoundComplete );
			//sound.load();
		}
		
		override protected function addListeners() : void
		{
			super.addListeners();
			if ( __playBtn )
				__playBtn.addEventListener( MouseEvent.MOUSE_UP , onPlayHandler );
			else
				Debug.log("播放按钮获取不到!" , this , Debug.LEVEL_FATAL );
		}
		
		override protected function removeListeners() : void
		{
			super.removeListeners();
			if ( __playBtn )
				__playBtn.removeEventListener( MouseEvent.MOUSE_UP , onPlayHandler );
		}
		
		// ---------------------------------- Public Methods ---------------------------------- //
		
		override public function active() : void
		{
			super.active();
			sound.gotoSoundTime( 0 );
			if ( !isClick )
				reset();
		}
		
		override public function cancel() : void
		{
			super.cancel();
			sound.pauseSound();
			if (__playBtn) {
				__playBtn.filters = [ Question.grayFilter ];
				__playBtn.mouseEnabled = false;
			}
		}
		
		override public function reset() : void
		{
			super.reset();
			repeat = getData( "repeat" ) as int;
			isClick = false;
			if (__playBtn) {
				__playBtn.filters = [];
				__playBtn.mouseEnabled = true;
			}
		}
		
		override public function check() : Boolean
		{
			return true;
		}
		
		// ---------------------------------- Event Methods ---------------------------------- //
		
		/**
		 * sound loader complete
		 * @param	e
		 */
		private function onComplete( e : LoaderEvent ) : void
		{
			sound.removeEventListener( LoaderEvent.COMPLETE , onComplete );
			sound.removeEventListener( LoaderEvent.ERROR , onError );
			Debug.log( "加载声音成功: " + url , this );
		}
		
		/**
		 * sound loader error
		 * @param	e
		 */
		private function onError( e : LoaderEvent ) : void
		{
			sound.removeEventListener( LoaderEvent.COMPLETE , onComplete );
			sound.removeEventListener( LoaderEvent.ERROR , onError );
			Debug.log( "加载声音错误: " + e.text , this , Debug.LEVEL_ERROR );
		}
		
		/**
		 * play button click
		 * @param	e
		 */
		private function onPlayHandler( e : MouseEvent ) : void
		{
			sound.load();
			sound.gotoSoundTime( 0 , true );
			removeListeners();
			isClick = true;
			__playBtn.mouseEnabled = false;
			__playBtn.filters = [ Question.grayFilter ];
		}
		
		/**
		 * sound play complete
		 * @param	e
		 */
		private function onSoundComplete( e : LoaderEvent ) : void
		{
			repeat--;
			if ( repeat > 0 )
				sound.gotoSoundTime( time , true );
			//Debug.log( "sound title: " + time + " s repeat: " + repeat , this , Debug.LEVEL_DEBUG );
		}
	}

}